(function () {
    'use strict';

    angular
        .module('app.patientdemo')
        .controller('patientdemoController', patientdemoController);

    patientdemoController.$inject = ['logger', '$state', '$rootScope', '$stateParams', 'PatientDemoData', '$http', 'DefaultListsProvider', '$mdDialog','$timeout'];
    /* @ngInject */
    function patientdemoController(logger, $state, $rootScope, $stateParams, PatientDemoData, $http, DefaultListsProvider, $mdDialog,$timeout) {
        var vm = this;
        vm.title = 'patientdemo';
        vm.user = $stateParams.Category;
        vm.password = "";
        vm.ReferralID = $stateParams.ReferralID;
        vm.EditClick = EditClick;
        //vm.patientdemoClick = patientdemoClick;
        vm.ReadOnly = true;
        vm.SavePatientData = SavePatientData;
        vm.ShowControls = true;
        vm.FilterPractice = FilterPractice;
        vm.FilterPracticePPG = FilterPracticePPG;
        activate();
        
        function EditClick() {
            vm.ReadOnly = false;
        }

        function activate() {

            vm.Physicians = DefaultListsProvider.GetPhysiciansList();
            vm.PracticesList = DefaultListsProvider.GetPracticesList();
            vm.StateList = DefaultListsProvider.GetStateList();
            vm.ReferringPhysicians = vm.Physicians;//DefaultListsProvider.GetReferringPhysiciansList();

            var promise = PatientDemoData.GetPatientDemo(vm.ReferralID).then(function (response) {
                vm.PatientID = response.data.PatientID;
                vm.FirstName = response.data.PatientFirstName;
                vm.LastName = response.data.PatientLastName;
                
                vm.DOB = new Date(response.data.DOB);
                
                vm.AddrLine1 = response.data.AddrLine1;
                vm.AddrLine2 = response.data.AddrLine2;
                vm.PatientState = response.data.State;
                vm.zipcode = response.data.ZipCode;
                vm.phno = response.data.PhoneNumber;
                vm.phonetype = response.data.PhoneType;
                vm.Email = response.data.Email;
                vm.InsuranceInfo = response.data.Insurance;
                vm.PlanNumber = response.data.PlanNumber;
                vm.InsuranceInfo1 = response.data.Insurance1;
                vm.PlanNumber1 = response.data.PlanNumber1;
                vm.Notes = response.data.Notes;
                vm.ReferringPhysician = response.data.PhysicianID;
                vm.ReqPractice = response.data.RequestedPractice;
                vm.PreferredPhysician = response.data.PreferredPhysician;
                vm.RefReason = response.data.RefReason;
                vm.StatRequest = response.data.IsUrgentRequest;
                vm.PrefPhone = response.data.PrefPhone;
                vm.EstablishedPatient = response.data.EstablishedPatient;
                vm.OtherPhysician = !(response.data.PPGPhysician);
            });

            $mdDialog.hide();
        
            //logger.info('Activated patientdemo View');
        }


        function FilterPractice(Phy) {
            return Phy.CorporatePhysician == 'false' && Phy.PracticeID == vm.ReqPractice;
        }

        function FilterPracticePPG(Phy) {
            return Phy.CorporatePhysician == 'true' && Phy.PracticeID == vm.ReqPractice;
        }


        function SavePatientData()
        {
            
            var promise1 = PatientDemoData.SavePatientData(vm.PatientID, vm.FirstName, vm.LastName, vm.DOB.format("mm/dd/yy"), vm.AddrLine1, vm.AddrLine2, vm.PatientState, vm.zipcode, vm.phno, vm.phonetype, vm.Email, vm.InsuranceInfo, vm.PlanNumber, vm.InsuranceInfo1, vm.PlanNumber1, vm.Notes, vm.ReqPractice, vm.ReferringPhysician, vm.PreferredPhysician, $rootScope.CurrentLoginID, vm.RefReason, vm.StatRequest, vm.PrefPhone, vm.EstablishedPatient).then(function (response) {
                
                if (response.data.SaveStatus == 'Saved')
                {
                    vm.ReadOnly = true;
                    $mdDialog.show(
                          $mdDialog.alert()
                            .parent(angular.element(document.body))
                            .clickOutsideToClose(true)
                            .title('Save Status')
                            .textContent('Patient Demographics have been saved.')
                            .ariaLabel('Save Status Dialog')
                            .ok('Close')
                        ).then(function (response) {
                            $timeout(function () {
                                angular.element('#BackButton').triggerHandler('click');
                            }, 100);
                        });
                }
            $mdDialog.hide();
            });

        }

        function showWait() {
            $mdDialog.show({
                controller: 'patientdemoController',
                template: '<md-dialog style="background-color:transparent;box-shadow:none">' +
                            '<div layout="row" layout-sm="column" layout-align="center center" aria-label="wait">' +
                                '<md-progress-circular md-mode="indeterminate" class="md-accent"></md-progress-circular>' +
                            '</div>' +
                         '</md-dialog>',
                parent: angular.element(document.body),
                clickOutsideToClose: false,
                fullscreen: false
            })
            .then(function (answer) {

            });
        };
    }
})();


